package cn.sx.controller;

import cn.sx.entity.Goods;
import cn.sx.entity.Tops;
import cn.sx.entity.Type;
import cn.sx.service.GoodsService;
import cn.sx.service.TypesService;
import cn.sx.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 *@program:sx_emall_ssm
 *@author: sx
 *@desc: 前台相关接口
 *@Time: 2020/9/22  13:51
 */
@Controller
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private TypesService typesService;

    /*商品分类（显示商品）*/
    @GetMapping("/type")
   public String type(HttpServletRequest request,
                      @RequestParam(required=false, defaultValue="0")int id,
                      @RequestParam(required = false,defaultValue = "1")int page,
                      @RequestParam(required = false,defaultValue = "10")int size)//一页显示多少数据
   {
        request.setAttribute("type",typesService.get(id));
        request.setAttribute("goodList",goodsService.getListByType(id,page,size));
        request.setAttribute("pageHtml", PageUtil.getPageHtml(request,goodsService.getCountByType(id),page,size));
        return "/index/goods.jsp";
   }

    /*主页显示*/
    @GetMapping("/index")
    public String Index(HttpServletRequest request) {
        request.setAttribute("flag",1);
        /*今日推荐*/
        List<Goods> todayList=goodsService.getListByTopType(Tops.TYPE_TODAY,1,6);//取前六个
        request.setAttribute("todayList",todayList);
        /*热销排行*/
        List<Goods> hotList = goodsService.getListOrderSales(1,10);//取前十个
        request.setAttribute("hotList",hotList);
        /*类目列表*/
        List<Type> typeList = typesService.findAll();
        List<Map<String,Object>> dataList = new ArrayList<>();
        for(Type type : typeList){
            Map<String,Object> map = new HashMap<>();
            map.put("type",type);
            map.put("goodList",goodsService.getListByType(type.getId(),1,15));//取前十五个
            dataList.add(map);
        }
        request.setAttribute("dataList",dataList);
        return "index.jsp";
    }

   /*今日推荐*/
    @GetMapping("/today")
    public String today(HttpServletRequest request,
                        @RequestParam(required = false,defaultValue = "1")int page,
                        @RequestParam(required = false,defaultValue = "10")int size){
            request.setAttribute("flag",2);
            request.setAttribute("goodList",goodsService.getListByTopType(Tops.TYPE_TODAY,page,size));
            request.setAttribute("pageHtml",PageUtil.getPageHtml(request,goodsService.getCountByTopType(Tops.TYPE_TODAY),page,size));
            return "/index/goods.jsp";
    }
    /*热销排行*/
    @GetMapping("/hot")
    public String hot(HttpServletRequest request,
                      @RequestParam(required = false,defaultValue = "1")int page,
                      @RequestParam(required = false,defaultValue = "10")int size){
        request.setAttribute("flag",3);
        request.setAttribute("goodList",goodsService.getListOrderSales(page,size));
        request.setAttribute("pageHtml",PageUtil.getPageHtml(request,goodsService.getCount(),page,size));
        return "/index/goods.jsp";
    }
    /*新品上市*/
    @GetMapping("/new")
    public String newGood(HttpServletRequest request,
                          @RequestParam(required = false,defaultValue = "1")int page,
                          @RequestParam(required = false,defaultValue = "10")int size){
        request.setAttribute("flag",4);
        request.setAttribute("goodList",goodsService.getList(page,size));
        request.setAttribute("pageHtml",PageUtil.getPageHtml(request,goodsService.getCount(),page,size));
        return "/index/goods.jsp";
    }
    /*跳转商品详情界面*/
    @RequestMapping("/detail")
    public String detail(int id,HttpServletRequest request){
        request.setAttribute("good",goodsService.get(id));
        //今日推荐前两个  在详情页显示
        request.setAttribute("todayList",goodsService.getListByTopType(Tops.TYPE_TODAY,1,2));
        return "/index/detail.jsp";
    }




}
